// 构造函数：用于初始化对象
// new 原理
    /*
        1. 创建新对象
        2. 获取构造函数的prototype
        3. 将构造函数的prototype属性指向新建对象
        4. 返回新建对象
    */


    function myNew() {
        let newObj = {};

        let constructor = Array.prototype.shift.call(arguments);
        let res = null;

        if (typeof constructor !== 'function') {
            throw new TypeError('constructor must be a function')
        };

        newObj = Object.create(constructor.prototype);
        res = constructor.apply(newObj, arguments);

        let flag = res && (typeof res === 'object' && typeof res === 'function');
        return flag ? res : newObj;
    };

    function Person(name, age) {
        this.name = name;
        this.age = age;
    };


    let p1 = myNew(Person, 'Katrina', 18);

    console.log(p1);   // Person { name: 'Katrina', age: 18 }